home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 18014 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  7.6 KB

  1. Path: news.sprintlink.net!datalytics!usenet
  2. From: Rob Stewart <stew@datalytics.com>
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: help with homework
  5. Date: Thu, 18 Apr 1996 11:21:38 -0400
  6. Organization: Datalytics, Inc
  7. Message-ID: <31765E02.16BF@datalytics.com>
  8. References: <4l1k3i$kle@dfw-ixnews2.ix.netcom.com>
  9. NNTP-Posting-Host: 204.62.224.71
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 2.0 (WinNT; I)
  14.  
  15. leonel wizel wrote:
  16. >     Please I need help to complete this file:   this file suppose to
  17. > increment the object time by one second to the next hour, to the
  18. > next minute to the next day:
  19. > I need some help please:
  20. > the program suppose to modify to include a tick member function
  21. > that increments the time stored in the Time object by one second.  The
  22. > time object should always remain in a consistent state.  The driver
  23. > program that tests the tick member function in a loop that prints the
  24. > time in standard format during each iteration of the loop to illustrate
  25. > that the tick member functions workds correctly.
  26. >     Incrementing into the next minute.
  27. >     Incrementing into the next hour.
  28. >     incrementing into the next day(i.e., 11:59:59 PM to 12:00:00 AM).
  29. > //TIME3.H
  30. > #ifndef TIME3_H
  31. > #define TIME3_H
  32. > class Time {
  33. > public:
  34. >         Time(int = 0, int = 0, int = 0);  // constructor
  35. >         //set functions
  36.  
  37. setTime should probably return a boolean to indicate if the time 
  38. was valid, and only set the time if the input was valid.
  39.  
  40. >         void setTime(int, int, int); //set hour, minute, second
  41.  
  42. Do you really need the following three member functions?  The 
  43. setTime member function seems the most logical, since it can 
  44. handle validating all values at once.  setTime could handle 
  45. overflow of any values (like excess seconds increment minute) if 
  46. you like.
  47.  
  48. >         void setHour(int);  //set hour
  49. >         void setMinute(int); //set minute
  50. >         void setSecond(int); //set second
  51. >         //get functions
  52. >         int getHour();   //return hour
  53. >         int getMinute(); //return minute
  54. >         int getSecond(); //return second
  55. >         void tick_increment();
  56.  
  57. It hardly seems appropriate for a Time class to know how to 
  58. print.  Rather, it should return a string (or populate a 
  59. supplied buffer) with the formatted time that the class user 
  60. could print, if desired.
  61.  
  62. Also, consider an enumerated type to indicate the desired 
  63. format:
  64.  
  65.     enum time_format
  66.     {
  67.         MILITARY,
  68.         STANDARD
  69.     };
  70.  
  71.     void    format(
  72.             char* o_pBuffer,
  73.             size_t i_BufferLength,
  74.             time_format i_Format);
  75.  
  76. Of course this doesn't tell the caller how big the buffer needs 
  77. to be, and doesn't indicate if the formatting was successful.  
  78. I'll leave that part to your imagination.
  79.  
  80. >         void printMilitary();   //output military time
  81. >         void printStandard();   //output standard time
  82. > private:
  83.  
  84. Why are you using signed types for inherently unsigned values?  
  85. How about unsigned char?
  86.  
  87. >         int hour;          //0 - 23
  88. >         int minute;        //0 - 59
  89. >         int second;        //0 - 59
  90. > };
  91. > #endif
  92. > #include "time3.h"
  93. > #include <iostream.h>
  94. > //constructor function
  95. > Time::Time(int hr, int min, int sec) {setTime (hr, min, sec); }
  96.  
  97. The action you take here is a little odd.  Instead of forcing 
  98. values to zero if the input is invalid, how about return a 
  99. boolean to indicate whether you set the time:
  100.  
  101. bool Time::setTime(
  102.     unsigned char i_Hour,
  103.     unsigned char i_Minute,
  104.     unsigned char i_Second)
  105. {
  106.     bool bResult = FALSE;
  107.     if (i_Hour < 24 && i_Minute < 60 && i_Second < 60)
  108.     {
  109.         hour = i_Hour;
  110.         minute = i_Minute;
  111.         second = i_Second;
  112.         bResult = TRUE;
  113.     }
  114.     return bResult;
  115. }
  116.  
  117. > void Time::setTime( int h, int m, int s)
  118. > {
  119. >         hour = (h >= 0 && h < 24) ? h : 0;
  120. >         minute = (m >= 0 && m < 60) ? m : 0;
  121. >         second = (s >= 0 && s < 60) ? s : 0;
  122. > }
  123. > void Time::setHour(int h)  { hour = (h >= 0 && h < 24) ? h : 0;}
  124. > void Time::setMinute(int m)
  125. >         { minute = (m >= 60) ? m : 0; }
  126. > void Time::setSecond(int s)
  127. >         { second = (s >= 60) ? s : 0; }
  128. > int Time::getHour() {return hour; }
  129. > int Time::getMinute() {return minute; }
  130. > int Time::getSecond() {return second; }
  131. > void Time::tick_increment()
  132. > {
  133. >         if(second == 60)
  134. >         {
  135. >                 second = 0;
  136. >                 minute++;
  137. >         }
  138. >                 second++;
  139.  
  140. Doesn't this next part look familiar?  Why not increment second 
  141. first, then adjust both second and minute as needed?
  142. >                 if (second == 60)
  143. >         {
  144. >                 second = 0;
  145. >                 minute++;
  146. >         }
  147. >                 if(minute == 60)
  148. >         {
  149. >                         minute = 0;
  150. >                         hour++;
  151. >         }
  152. >                 if (hour == 24)
  153. >         {
  154.  
  155. Modulo works here, but it involves a divide unecessarily.  Why 
  156. not just subtract 24?
  157.  
  158.             hour -= 24;
  159.  
  160. >                         hour %= 24;
  161. >         }
  162. > }
  163.  
  164. Try this instead for tick_increment (wouldn't tick be a better 
  165. name since it's like the tick of a clock?):
  166.  
  167. void Time::tick(void)
  168. {
  169.     second++;
  170.     if (second > 59)
  171.     {
  172.         second -= 60;
  173.         minute++;
  174.     }
  175.     if (minute > 59)
  176.     {
  177.         minute -= 60;
  178.         hour++;
  179.     }
  180.     if (hour > 23)
  181.     {
  182.         hour -= 24;
  183.     }
  184. }
  185.  
  186. > void Time::printMilitary()
  187. > {
  188.  
  189. Don't get so enamored of the ternary operator.  Your code is 
  190. less efficient when you insert a null string into cout.  Try 
  191. this instead:
  192.  
  193.     if (hour < 10)
  194.     {
  195.         cout << '0';
  196.     }
  197.     cout << hour << ':';
  198.     if (minute < 10)
  199.     {
  200.         cout << '0';
  201.     }
  202.     cout << minute << ':';
  203.     if (second < 10)
  204.     {
  205.         cout << '0';
  206.     }
  207.     cout << second;
  208.  
  209. When you do this, do you notice how similar all of this code is?  
  210. In other words, we need a member function to capture this 
  211. commonality:
  212.  
  213. void Time::printNumber(
  214.     int i_Number)
  215. {
  216.     if (i_Number < 10)
  217.     {
  218.         cout << '0';
  219.     }
  220.     cout << i_Number;
  221. }
  222.  
  223. So now, printMilitary becomes this:
  224.  
  225.     printNumber(hour);
  226.     cout << ':';
  227.     printNumber(minute);
  228.     cout << ':';
  229.     printNumber(second);
  230.  
  231. >         cout <<  (hour < 10 ? "0" : "") << hour << ":"
  232. >                   << (minute < 10 ? "0" : "") << minute << ":"
  233. >                   << (second < 10 ? "0" : "") << second;
  234. > }
  235. > void Time::printStandard()
  236. > {
  237. >         cout << ((hour == 0 || hour == 12) ? 12 : hour %12) << ":"
  238. >                   << (minute < 10 ? "0" : "") << minute << ":"
  239. >                   << (second < 10 ? "0" : "") << second
  240. >                   << (hour < 12 ? " AM" : " PM");
  241. > }
  242. > #include <iostream.h>
  243. > #include "time3.h"
  244. > #include "time3.cpp"
  245. > main()
  246. > {
  247. >   Time t;
  248. >   t.setTime(23, 59, 59);
  249. >   for(long int i = 1; i <= 2; i++)
  250. >         {
  251. >                 cout << endl << endl;
  252. >                 cout << " Hour " << t.getHour()
  253. >                           << " Minute " << t.getMinute()
  254. >                           << " Second " << t.getSecond();
  255. >                 cout <<"the initial time before incrementing is: " << endl
  256. > << endl;
  257. >                 t.printStandard();
  258. >                 cout << endl;
  259. >                 t.printMilitary();
  260. >                 t.tick_increment();
  261. >                 cout << " Hour " << t.getHour()
  262. >                           << " Minute " << t.getMinute()
  263. >                           << " Second " << t.getSecond()
  264. >                           <<" the time after incrementing is: " << endl;
  265. >                 t.printStandard();
  266. >                 cout << endl;
  267. >                 t.printMilitary();
  268. >         }
  269. >         return 0;
  270. > }
  271.  
  272. Having said all that, I have to ask, what's wrong with the code?  
  273. I tested it and it seems to work correctly.  Was there something 
  274. specific you noticed that was wrong?
  275.  
  276. -- 
  277. Robert Stewart        | My opinions are usually my own.
  278. Datalytics, Inc.    | stew@datalytics.com
  279.